package com.homedo.odin.solution.service;

import com.homedo.core.common.base.service.BaseService;
import com.homedo.odin.solution.contract.pc.UserUpdateValidRequestBean;
import com.homedo.odin.solution.dao.UserProfileMapper;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.enumcode.user.RoleEnum;
import com.homedo.odin.solution.model.entity.UserProfile;
import org.springframework.stereotype.Service;

/**
 * <p> 用户简介 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class UserProfileServiceImpl extends BaseService<UserProfileMapper, UserProfile> {


    /**
     * 根据用户id查找信息
     */
    public UserProfile selectByUserId(Integer userId) {
        UserProfile userProfile = new UserProfile();
        userProfile.setUserId(userId);
        userProfile.setIsDel(0);
        UserProfile profile = baseMapper.selectOne(userProfile);
        return profile;
    }

    /**
     * 根据userID更新banner
     */
    public Integer updateBannerByUserId(Integer userId, String banner) {
        return baseMapper.updateBannerByUserId(userId, banner);
    }

    /**
     * 更新个人简介
     */
    public Integer updateIntroductionByUserId(Integer userId, String introduction) {
        return baseMapper.updateIntroductionByUserId(userId, introduction);
    }

    /**
     * 主页浏览量默认加1
     */
    public void updateViewCount(Integer userId, Integer visitorId, Integer add) {
        baseMapper.updateViewCount(userId, visitorId, add);
    }

    /**
     * 根据userID更新人气值
     */
    public void updateClickCount(Integer targetId, Integer operationId, Integer score) {
        baseMapper.updateClickCount(targetId, operationId, score);
    }

    public UserProfile findByUserId(Integer userId) {
        UserProfile userProfile = new UserProfile();
        userProfile.setUserId(userId);
        userProfile.setIsDel(0);
        return baseMapper.selectOne(userProfile);
    }

    /**
     * 判断是否添加user_profile
     * @param userUpdateRequest
     */
  public void updateOrInsert(UserUpdateValidRequestBean userUpdateRequest) {
      Integer userId = userUpdateRequest.getUserId();
      UserProfile userProfileReq = new UserProfile();
      userProfileReq.setUserId(userId);
      UserProfile userProfile  = baseMapper.selectOne(userProfileReq);
      //userProfile没有该用户，则插入
      if(userProfile==null){
          userProfile = new UserProfile();
          userProfile.setUserName(userUpdateRequest.getUserName());
          userProfile.setUserId(userUpdateRequest.getUserId());
          userProfile.setRole(RoleEnum.NORMALUSER.getDbValue());
          userProfile.setAddress(userUpdateRequest.getAddress());
          userProfile.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
          userProfile.setCreatedTime(new java.util.Date());
          userProfile.setModifiedTime(new java.util.Date());
          baseMapper.insert(userProfile);
      }else{
          userProfile.setAddress(userUpdateRequest.getAddress());
          userProfile.setUserName(userUpdateRequest.getUserName());
          baseMapper.updateById(userProfile);
      }
  }
}
